package com.erp.gather.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.gather.domain.ErpCollectProductTask20;
import com.erp.gather.domain.bo.ErpCollectProductTask20Bo;
import com.erp.gather.domain.vo.ErpCollectProductTask20Vo;
import com.erp.gather.mapper.ErpCollectProductTask20Mapper;
import com.erp.gather.service.IErpCollectProductTask20Service;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 采集商品任务2.0Service业务层处理
 *
 * @author erp
 * @date 2023-08-28
 */
@RequiredArgsConstructor
@Service
public class ErpCollectProductTask20ServiceImpl implements IErpCollectProductTask20Service {

    private final ErpCollectProductTask20Mapper baseMapper;

    /**
     * 查询采集商品任务2.0
     */
    @Override
    public ErpCollectProductTask20Vo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询采集商品任务2.0列表
     */
    @Override
    public TableDataInfo<ErpCollectProductTask20Vo> queryPageList(ErpCollectProductTask20Bo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpCollectProductTask20> lqw = buildQueryWrapper(bo);
        Page<ErpCollectProductTask20Vo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询采集商品任务2.0列表
     */
    @Override
    public List<ErpCollectProductTask20Vo> queryList(ErpCollectProductTask20Bo bo) {
        LambdaQueryWrapper<ErpCollectProductTask20> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpCollectProductTask20> buildQueryWrapper(ErpCollectProductTask20Bo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpCollectProductTask20> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCollectUrl()), ErpCollectProductTask20::getCollectUrl, bo.getCollectUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getCategory()), ErpCollectProductTask20::getCategory, bo.getCategory());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), ErpCollectProductTask20::getStatus, bo.getStatus());
        lqw.eq(bo.getIsOptimize() != null, ErpCollectProductTask20::getIsOptimize, bo.getIsOptimize());
        lqw.eq(bo.getIsTranslate() != null, ErpCollectProductTask20::getIsTranslate, bo.getIsTranslate());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpCollectProductTask20::getRevision, bo.getRevision());
        lqw.eq(StringUtils.isNotBlank(bo.getCollectDuration()), ErpCollectProductTask20::getCollectDuration, bo.getCollectDuration());
        lqw.eq(StringUtils.isNotBlank(bo.getCollectBy()), ErpCollectProductTask20::getCollectBy, bo.getCollectBy());
        lqw.eq(bo.getCollectTime() != null, ErpCollectProductTask20::getCollectTime, bo.getCollectTime());
        lqw.eq(bo.getCollectUserId() != null, ErpCollectProductTask20::getCollectUserId, bo.getCollectUserId());
        return lqw;
    }

    /**
     * 新增采集商品任务2.0
     */
    @Override
    public Map<String, Object> insertByBo(ErpCollectProductTask20Bo bo) {
    	Map<String, Object> map = new HashMap<>();
        List<String> alreadyUrls = new ArrayList<>();
        List<String> errorCategoryUrls = new ArrayList<>();
        Integer count = 0;
        String[] urls = bo.getCollectUrl().replace("\n", ",").split(",");
        for (String url : urls) {
            try {
                if (Strings.isBlank(url)) {
                    continue;
                }
//                if (url.contains("https://www.pangbosupply.com/") || url.contains("http://www.pangbosupply.com/") || url.contains("http://pangbosupply.com/")) {
//                    url = url.contains("&goods_id=") ? url.substring(0, url.indexOf("&goods_id=")) : url;
//                } else {
//                    url = url.substring(0, url.indexOf(".html") + 5);
//                }
                if (baseMapper.selectCount(
                        Wrappers.lambdaQuery(new ErpCollectProductTask20())
                                .like(ErpCollectProductTask20::getCollectUrl, url)
                ) > 0) {
                    alreadyUrls.add(url);
                    continue;
                }
                if (Objects.equals(bo.getCategory(), "1")) {
                    if (!url.contains(".aliexpress.")) {
                        errorCategoryUrls.add(url);
                        continue;
                    }
                }
                if (Objects.equals(bo.getCategory(), "2")) {
                    if (!url.contains("detail.1688.com/")) {
                        errorCategoryUrls.add(url);
                        continue;
                    }
                }
//                if (Objects.equals(bo.getCategory(), "3")) {
//                    if (!url.contains("pangbosupply.com/list/detail")) {
//                        errorCategoryUrls.add(url);
//                        continue;
//                    }
//                }
                if (Objects.equals(bo.getCategory(), "3")) {
                    if (!url.contains("b2csupply.com/list/detail")) {
                        errorCategoryUrls.add(url);
                        continue;
                    }
                }
                ErpCollectProductTask20 add = BeanUtil.toBean(bo, ErpCollectProductTask20.class);
                add.setCollectUrl(url);
                baseMapper.insert(add);
                count++;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        map.put("count", count);
        map.put("urls", alreadyUrls);
        map.put("errorCategoryUrls", errorCategoryUrls);

        return map;
    }

    /**
     * 修改采集商品任务2.0
     */
    @Override
    public Boolean updateByBo(ErpCollectProductTask20Bo bo) {
        ErpCollectProductTask20 update = BeanUtil.toBean(bo, ErpCollectProductTask20.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpCollectProductTask20 entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除采集商品任务2.0
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
